"""配置管理类"""
import os
from typing import Optional, Dict, Any, Union
from dotenv import load_dotenv


class ConfigManager:
    _instance: Optional['ConfigManager'] = None
    _config: Dict[str, str] = {}
    _defaults = {
        'base_url': 'https://api.dify.ai/v1',
        'user_id': 'default_user',
        'request_timeout': '30',
        'typing_speed': '0.01',
        'log_level': 'INFO'  # 添加日志级别默认值
    }

    def __new__(cls):
        if cls._instance is None:
            cls._instance = super().__new__(cls)
            cls._load_config()
        return cls._instance

    @classmethod
    def _load_config(cls) -> None:
        """加载配置"""
        load_dotenv()
        cls._config = {
            'chat_api_key': os.getenv('DIFY_CHAT_API_KEY'),
            'base_url': os.getenv('DIFY_API_URL', cls._defaults['base_url']),
            'user_id': os.getenv('DIFY_USER_ID', cls._defaults['user_id']),
            'request_timeout': os.getenv('DIFY_REQUEST_TIMEOUT', cls._defaults['request_timeout']),
            'typing_speed': os.getenv('DIFY_TYPING_SPEED', cls._defaults['typing_speed']),
            'log_level': os.getenv('DIFY_LOG_LEVEL', cls._defaults['log_level'])  # 添加日志级别配置获取
        }

    @classmethod
    def get_config(cls, key: str, default: str = '') -> str:
        """获取配置值"""
        if key not in cls._config:
            cls._load_config()
        return cls._config.get(key, default)
